//
//  HWGameIcoPopView.m
//  HWPromoteProjectAPP
//
//  Created by 陈道斌 on 2020/2/28.
//  Copyright © 2020 陈道斌. All rights reserved.
//

#import "HWGameIcoPopView.h"
#import "HWGameYouXitanKuangCollectionViewCell.h"




@interface HWGameIcoPopView()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property(strong,nonatomic) UIView *grayView;
@property(strong,nonatomic) UIView *fenxiangView;

@property(strong,nonatomic) UICollectionView *maincollectionView;
@property(strong,nonatomic) NSMutableArray *dataMuArr;

@property(strong, nonatomic) UIView *searchBgView;
@property(strong, nonatomic) UIImageView *searchImg;
@property(strong, nonatomic) UITextField *searchField;
@property(strong, nonatomic) UIButton *searchButton;
@property(strong, nonatomic) NSString *keyword;

@end


@implementation HWGameIcoPopView

-(instancetype)initWithFrame:(CGRect)frame {
    
    
    if (self = [super initWithFrame:frame]) {
        self.keyword = @"";
        // 添加灰色背景
        [self addgrayView];

        [self setupUI];
        
        //添加conview
        [self addCommonControlsView];
        
    }
    
    
    return  self;
    
    
}


// 请求数据
- (void)requestDatashuju {
    
    
    
           NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
           NSString *uid =  [HWLocallyStoredData getLoginUserID];
           NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tStr,@"adesxloksy13s98adv"];
           NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
             
        
    
    NSDictionary *parametes = @{      @"ios" : @"1",
                                      @"ct" : @"app",
                                      @"ac" : @"fuli_game",
                                      @"t" : tStr,
                                      @"uid":uid,
                                      @"sign" :signStrMd5,
                                      @"keyword":self.keyword
    };
          
          
              [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
                    
                   

                    NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
                    if ([codeStr intValue] == 1) {
                   
                       NSLog(@"%@",response);
                        
                  //    HWdaichongzheKouModel
                        
                        
                   //     [self.mainTabelView reloadData];
                        
                        self.dataMuArr = [HWdaichongzheKouModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
                   
                        [self.maincollectionView reloadData];
                  
                  

                         
                    } else {
                        
                        [MBProgressHUD showError:response[@"message"]];
                                      
                  
                        
                    }
                    
               
                    
                } failure:^(NSString *error) {
                   // [SVProgressHUD dismiss];
                    
                    [MBProgressHUD showSuccess:@"登录失败"];
                    [MBProgressHUD hideHUD];
                    
                    
                    
                    
                }];
    
    
    
}



// 添加灰色View
-(void)addgrayView {
    
    self.grayView = [[UIView alloc]init];
    self.grayView.frame  = CGRectMake(0,0, kScreenW, kScreenH);
    
    self.grayView.backgroundColor =  RGB(51, 51, 51);
    self.grayView.alpha = 0.6;
    [self addSubview:self.grayView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapView:)];
    //  tap.delegate = self;
    [self.grayView addGestureRecognizer:tap];
    
    
    
    
}


//  设置UI
- (void)setupUI {
    
    
    
       self.fenxiangView = [[UIView alloc]initWithFrame:CGRectMake(20*CFLScaleValue, (kScreenH-430)/2,kScreenW-40*CFLScaleValue, 430)];
       self.fenxiangView.backgroundColor = RGB(240, 244, 246);
       self.fenxiangView.backgroundColor = [UIColor whiteColor];
        self.fenxiangView.layer.cornerRadius = 8;
       self.fenxiangView.layer.masksToBounds = YES;
       [self addSubview:self.fenxiangView];
    
    
    
    
}



//添加一些常用的控件
- (void)addCommonControlsView {
    
       
    
        UILabel *selectLabel = [[UILabel alloc]initWithFrame:(CGRect)CGRectMake(10,20, kScreenW-20*CFLScaleValue*2, 25)];
        selectLabel.font = [UIFont systemFontOfSize:18];
        selectLabel.textColor = RGB(68, 68, 68);
        selectLabel.text = @"游戏图标";
        selectLabel.textAlignment =  NSTextAlignmentCenter;
        [self.fenxiangView  addSubview:selectLabel];
    
    self.searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.searchButton setTitle:@"搜索" forState:UIControlStateNormal];
    self.searchButton.titleLabel.font = [UIFont systemFontOfSize:14*CFLScaleValue];
    self.searchButton.backgroundColor = [UIColor blueColor];
    [self.searchButton addTarget:self action:@selector(searchButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.searchButton.layer.cornerRadius = 15*CFLScaleValue;
    [self.fenxiangView addSubview:self.searchButton];
    [self.searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-10*CFLScaleValue);
        make.height.mas_equalTo(30*CFLScaleValue);
        make.width.mas_equalTo(50*CFLScaleValue);
        make.top.mas_equalTo(selectLabel.mas_bottom).offset(10*CFLScaleValue);
    }];
    
    
    self.searchBgView = [[UIView alloc] init];
    self.searchBgView.layer.cornerRadius = 15*CFLScaleValue;
    self.searchBgView.layer.borderColor = UIColor.lightGrayColor.CGColor;
    self.searchBgView.layer.borderWidth = 1;
    [self.fenxiangView addSubview:self.searchBgView];
    [self.searchBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(18*CFLScaleValue);
        make.height.mas_equalTo(30*CFLScaleValue);
        make.top.mas_equalTo(selectLabel.mas_bottom).offset(10*CFLScaleValue);
        make.right.mas_equalTo(self.searchButton.mas_left).offset(-10*CFLScaleValue);
    }];
    
    self.searchImg = [[UIImageView alloc] init];
    self.searchImg.image = [UIImage imageNamed:@"sousujiemian_ico"];
    [self.searchBgView addSubview:self.searchImg];
    [self.searchImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(14*CFLScaleValue);
        make.left.mas_equalTo(10*CFLScaleValue);
        make.centerY.mas_equalTo(0);
    }];
    
    self.searchField = [[UITextField alloc] init];
    self.searchField.placeholder = @"请输入搜索内容";
    [self.searchBgView addSubview:self.searchField];
    [self.searchField addTarget:self action:@selector(searchFieldTextChange:) forControlEvents:UIControlEventEditingChanged];
    [self.searchField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.searchImg.mas_right).offset(10*CFLScaleValue);
        make.height.mas_equalTo(30*CFLScaleValue);
        make.right.mas_equalTo(-10*CFLScaleValue);
    }];
    
    
    
    
    
       UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
       layout.minimumLineSpacing = 18;
       CGFloat juanju  = (kScreenW - 20*CFLScaleValue*2-25*2*CFLScaleValue - 44*4*CFLScaleValue)/3;
       layout.minimumInteritemSpacing = juanju-1;
    //   layout.headerReferenceSize = CGSizeMake(kScreenW, 40);  //设置headerView大小
       self.maincollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 120, kScreenW - 20*CFLScaleValue*2, 430-80) collectionViewLayout:layout];
       // self.maincollectionView.showsHorizontalScrollIndicator = NO;
       self.maincollectionView.delegate = self;
       self.maincollectionView.dataSource = self;
       self.maincollectionView.alwaysBounceVertical = YES;
       self.maincollectionView.backgroundColor = [UIColor whiteColor];
      // self.maincollectionView.contentInset = UIEdgeInsetsMake(0, 15, 0, 15);
       self.maincollectionView.contentInset = UIEdgeInsetsMake(0, 20*CFLScaleValue, 0, 20*CFLScaleValue);
     //  [self.maincollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];  //  一定要设置
       [self.maincollectionView registerClass:[HWGameYouXitanKuangCollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
    //   [self.maincollectionView  registerNib:[UINib nibWithNibName:@"HWJinXuanCell" bundle:nil] forCellWithReuseIdentifier:@"cellID"];
       self.maincollectionView.userInteractionEnabled = YES;
       [self.fenxiangView  addSubview:self.maincollectionView];
    

}
- (void)searchButtonClick:(UIButton *)button {
    [self endEditing:YES];
    
    [self requestDatashuju];
    
}

- (void)searchFieldTextChange:(UITextField *)textField{
    self.keyword = textField.text;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return  self.dataMuArr.count;
}


//- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
//{
//    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
//
//
//
//
//    UILabel *selectLabel = [[UILabel alloc]initWithFrame:(CGRect)CGRectMake(10,12, 90, 16)];
//    selectLabel.font = [UIFont systemFontOfSize:15];
//    selectLabel.textColor = RGB(51, 51, 51);
//    selectLabel.text = @"精选推荐";
//    [headerView addSubview:selectLabel];
//
//
////
//    return headerView;
//}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HWGameYouXitanKuangCollectionViewCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
  //   cell.backgroundColor =[UIColor yellowColor];
//    HWGameListModel *mode = self.dataMuArr[indexPath.row+2];
//    cell.model = mode;

    cell.dataModel = self.dataMuArr[indexPath.row];
    
    
    if (indexPath.row%2 == 0) {
        cell.backgroundColor = [UIColor whiteColor];
    } else {
        
        cell.backgroundColor = [UIColor whiteColor];
    }
    return cell;
    
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(44*CFLScaleValue,44*CFLScaleValue+17+4);
    
    
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    DLog(@"12334");
    
//    HWGameDetailsViewController *gameDetails = [[HWGameDetailsViewController alloc]init];
//
//    HWGameListModel *mode = self.dataMuArr[indexPath.row+2];
//    gameDetails.gameId = mode.id;
//
    
 //   [self.navigationController pushViewController:gameDetails animated:YES];
    
    HWdaichongzheKouModel *model  =  self.dataMuArr[indexPath.row];
    
   
    [self.delegate HWGameIcoPopViewDelegate:model];
    
     [self removeFromSuperview];
    
    
    
}


















- (void)tapView:(UITapGestureRecognizer *)touch {
    
    [touch.view.superview removeFromSuperview];
    //   [[NSNotificationCenter defaultCenter] postNotificationName:@"notifyName1" object:nil];
    
    
}


@end
